const webpack = require('webpack');
const CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const packageInfo = require('./package.json');
var path = require('path');

module.exports = {
    entry: path.join(__dirname, 'entry.js'),
    output: {
        path: path.join(__dirname, 'build'),
        filename: 'bundle.js',
        chunkFilename: "[id].chunk.js",
        publicPath: "http://oi5imwae4.bkt.clouddn.com/tally"  // add to use cdn
    },
    devServer: {
        inline: true,
        port: 7777
    },

    module: {
        loaders: [
            {
                test: /\.styl$/,
                // loader: "style!css!stylus"
                loader: ExtractTextPlugin.extract('style', 'css!stylus')
            },
            {
                test: /\.css$/,
                // loader: "style!css"
                loader: ExtractTextPlugin.extract('style', 'css')
            },
            {
                test: /\.js|jsx$/,
                exclude: /node_modules/,
                loaders: ['babel']
            }
        ]
    },

    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
            PRODUCTION: JSON.stringify(true),
            API_PRODUCTION: JSON.stringify(true),
            VERSION: JSON.stringify(packageInfo.version),
            BROWSER_SUPPORTS_HTML5: true,
            TWO: "1+1",
            "typeof window": JSON.stringify("object")
        }),
        // http://www.cnblogs.com/haogj/p/5160821.html
        new HtmlWebpackPlugin({
            title: 'tally',
            filename: 'index.html',
            hash: true,
            inject: true,
            cache: true,
            minify: { //压缩HTML文件
                removeComments: true, //移除HTML中的注释
                collapseWhitespace: true //删除空白符与换行符
            }
        }),
        new CommonsChunkPlugin({
            filename: "commons.js",
            name: "commons"
        }),
        new ExtractTextPlugin('app.css'),
        new webpack.optimize.DedupePlugin(),    //去重
        new webpack.optimize.OccurenceOrderPlugin(),    //优化载入id
        new webpack.optimize.UglifyJsPlugin({   //压缩
            output: {
                comments: false,
            },
            compress: {
                warnings: false
            }
        }),
    ]
};
